package com.test.mchelepesa.activity_fragment_rice;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.test.mchelepesa.R;
import com.test.mchelepesa.activity_fragment_rice.base_rice.BaseActivity_rice;
import com.test.mchelepesa.util_rice.ThreadUtil_rice;
import com.test.mchelepesa.util_rice.http_rice.HttpManager_rice;
import com.test.mchelepesa.view_rice.widget_rice.TitleView_rice;

import java.util.ArrayList;

public class ServiceFeedbackActivity_rice extends BaseActivity_rice {

    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_feedback_rice);
        init();
    }

    private void init(){

        TitleView_rice titleView = findViewById(R.id.activity_service_feedback_view_title);
        setBackBtn_rice(titleView.getBackIb());

        initLicense(
                findViewById(R.id.activity_service_feedback_view_license),
                findViewById(R.id.activity_service_feedback_license_logo),
                findViewById(R.id.activity_service_feedback_license_company_name)
        );

        ArrayList<TextView> typeTvs = new ArrayList<TextView>(){{
            add(findViewById(R.id.feedback_type_1));
            add(findViewById(R.id.feedback_type_2));
            add(findViewById(R.id.feedback_type_3));
            add(findViewById(R.id.feedback_type_4));
            add(findViewById(R.id.feedback_type_5));
            add(findViewById(R.id.feedback_type_6));
        }};

        final String[] params = getResources().getStringArray(R.array.feedback_params);

        for (TextView typeTv1:typeTvs){
            typeTv1.setOnClickListener(v -> {
                for (TextView typeTv2:typeTvs){
                    if (typeTv1==typeTv2){
                        typeTv2.setBackgroundResource(R.drawable.ic_border_orange_rice_1dp_round_corner_orange_lll_20dp);
                        typeTv2.setTextColor(getColor(R.color.orange_rice));
                        type = params[typeTvs.indexOf(typeTv1)%params.length];
                    }else {
                        typeTv2.setBackgroundResource(R.drawable.ic_border_gray_ddd_1dp_round_corner_white_20dp);
                        typeTv2.setTextColor(getColor(R.color.gray_ddd));
                    }
                }
            });
        }

        EditText inputEt = findViewById(R.id.activity_service_feedback_et_input);
        Button submitBtn = findViewById(R.id.activity_service_feedback_btn_submit);

        submitBtn.setOnClickListener(v -> {
            String input = inputEt.getText().toString();
            if (TextUtils.isEmpty(type)||TextUtils.isEmpty(input)) {
                showToast_rice(getString(R.string.word_input_tip));
                return;
            }
            HttpManager_rice.getInstance().submitServiceFeedback(type,input);
        });

    }

    public void onSubmitServiceFeedback(){
        showToast_rice(getString(R.string.word_submit_success));
        ThreadUtil_rice.getInstance().postDelay(this::onBackPressed,1000);
    }

}